﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
using PetaPoco;

namespace DAL.MySQL
{
    public class xinxiDAL
    {
        public Database db = DbScoure.DataBase();

        public qxinxiModel GetQOne(int id)
        {
            return db.SingleOrDefault<qxinxiModel>(
                "select a.*,b.content,b.mcontent,c.title as ctitle from xinxi as a left join xbody as b on a.ID=b.xid left join category as c on a.classid=c.ID where a.ID=@0", id);
        }
        public qxinxiModel GetQOneByTitle(string title)
        {
            return db.SingleOrDefault<qxinxiModel>(
                "select a.*,b.content,b.mcontent,c.title as ctitle from xinxi as a left join xbody as b on a.ID=b.xid left join category as c on a.classid=c.ID where a.title=@0", title);
        }
        public xinxiEntity GetOne(int id)
        {
            return db.SingleOrDefault<xinxiEntity>("select * from xinxi where ID=@0", id);
        }

        public xinxiEntity GetOneByWhere(string where, string order, params object[] args)
        {
            var pagelist= db.Page<xinxiEntity>(1,1,"select * from xinxi where 1=1 " + where + " order by " + order,
                args);
            if (pagelist.TotalItems > 0)
                return pagelist.Items[0];
            return new xinxiEntity();
        }
        public List<cxinxiModel> GetList(string where, string order, params object[] args)
        {
            var list = db.Fetch<cxinxiModel>("select a.*,b.title as ctitle from xinxi as a left join category as b on a.classid=b.ID where 1=1 " + where + " order by " + order + " ,a.ID desc", args);
            return list;
        }

        public Page<cxinxiModel> GetPageList(int pagenum, int pagesize, string where, string order, params object[] args)
        {
            var pagelist = db.Page<cxinxiModel>(pagenum, pagesize, "select a.*,b.title as ctitle from xinxi as a left join category as b on a.classid=b.ID where 1=1 " + where + " order by " + order+" ,a.ID desc", args);
            return pagelist;
        }

        public int UpdateHits(int ID)
        {
            return db.Execute("update xinxi set hits=hits+1 where ID=@0",ID);
        }
        public int Update(object obj, int ID)
        {
            return db.Update("xinxi", "ID", obj, ID);
        }
        public qxinxiModel Save(qxinxiModel model)
        {
            xinxiEntity xinximodel=new xinxiEntity();
            xinximodel.ID = model.ID;
            xinximodel.title = model.title;
            xinximodel.classid = model.classid;
            xinximodel.flag = model.flag;
            xinximodel.pic = model.pic;
            xinximodel.pics = model.pics;
            xinximodel.ordnum = model.ordnum;
            xinximodel.url = model.url;
            xinximodel.bannerpic = model.bannerpic;
            xinximodel.skin = model.skin;
            xinximodel.page_title = model.page_title;
            xinximodel.mpage_title = model.mpage_title;
            xinximodel.page_key = model.page_key;
            xinximodel.mpage_key = model.mpage_key;
            xinximodel.page_desc = model.page_desc;
            xinximodel.mpage_desc = model.mpage_desc;
            xinximodel.body = model.body;
            xinximodel.jbody = model.jbody;
            xinximodel.ding = model.ding;
            xinximodel.cai = model.cai;
            xinximodel.hits = model.hits;
            xinximodel.author = model.author;
            xinximodel.source = model.source;
            xinximodel.addtime = model.addtime;
            xinximodel.updatetime = model.updatetime;
            xinximodel.sid = model.sid;
            xinximodel.mid = model.mid;
            xbodyEntity xbodymodel=new xbodyEntity();
            xbodymodel.content = model.content;
            xbodymodel.mcontent = model.mcontent;
            db.BeginTransaction();
            db.Save("xinxi", "ID", xinximodel);
            xbodymodel.xid = xinximodel.ID;
            if (model.ID == 0)
            {
                db.Save("xbody", "ID", xbodymodel);
            }
            else
            {
                var xmodel=db.SingleOrDefault<xbodyEntity>("select * from xbody where xid=" + model.ID);
                xmodel.content = model.content;
                xmodel.mcontent = model.mcontent;
                db.Update("xbody", "ID", xmodel);
            }
            db.CompleteTransaction();
            return model;
        }

        public bool Del(int id)
        {
            try
            {
                db.BeginTransaction();
                db.Delete("xinxi", "ID", null, id);
                db.Execute("delete from xbody where xid=@0", id);
                db.CompleteTransaction();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool Del(xinxiEntity model)
        {
            try
            {
                db.BeginTransaction();
                db.Delete("xinxi", "ID", model.ID);
                db.Execute("delete from xbody where xid=@0", model.ID);
                db.CompleteTransaction();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
